package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.BlockRuleRemindDTO;
import com.xbongbong.paas.pojo.dto.CallAudioTranslateDTO;
import com.xbongbong.paas.pojo.dto.CallCallLogListDTO;
import com.xbongbong.paas.pojo.dto.CallCallLogSaveDTO;
import com.xbongbong.paas.pojo.dto.CallCenterLoginDTO;
import com.xbongbong.paas.pojo.dto.CallCenterModuleDTO;
import com.xbongbong.paas.pojo.dto.CallCustomerDisturbDTO;
import com.xbongbong.paas.pojo.dto.CallFindRefDTO;
import com.xbongbong.paas.pojo.dto.CallMissedCallListDTO;
import com.xbongbong.paas.pojo.dto.CallMobileCodeSendDTO;
import com.xbongbong.paas.pojo.dto.CallMobileCodeVerifyDTO;
import com.xbongbong.paas.pojo.dto.CallRecordConfigDTO;
import com.xbongbong.paas.pojo.dto.CallRecordDynamicDTO;
import com.xbongbong.paas.pojo.dto.CallRecordLogSaveDTO;
import com.xbongbong.paas.pojo.dto.CallRecordStaticDTO;
import com.xbongbong.paas.pojo.dto.CallSyncDepartmentDTO;
import com.xbongbong.paas.pojo.dto.CallTokenRefreshDTO;
import com.xbongbong.paas.pojo.dto.CallUpdateModuleDTO;
import com.xbongbong.paas.pojo.vo.BlockRuleRemindVO;
import com.xbongbong.paas.pojo.vo.CallAudioTranslateVO;
import com.xbongbong.paas.pojo.vo.CallCallLogSaveVO;
import com.xbongbong.paas.pojo.vo.CallCenterLoginVO;
import com.xbongbong.paas.pojo.vo.CallCenterModuleVO;
import com.xbongbong.paas.pojo.vo.CallCustomerDisturbVO;
import com.xbongbong.paas.pojo.vo.CallFindRefVO;
import com.xbongbong.paas.pojo.vo.CallLogListVO;
import com.xbongbong.paas.pojo.vo.CallLogRecordStaticVO;
import com.xbongbong.paas.pojo.vo.CallMobileCodeSendVO;
import com.xbongbong.paas.pojo.vo.CallMobileCodeVerifyVO;
import com.xbongbong.paas.pojo.vo.CallRecordConfigVO;
import com.xbongbong.paas.pojo.vo.CallSyncDepartmentVO;
import com.xbongbong.paas.pojo.vo.CallTokenRefreshVO;
import com.xbongbong.paas.pojo.vo.CallUpdateModuleVO;
import com.xbongbong.paas.service.CallCenterCallLogService;
import com.xbongbong.paas.service.CallCenterCommonService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 话务中心 用户操作及普通功能 API 控制器
 * @date 2019/2/12 14:34
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter")
public class CallCenterCommonController {
    private static final Logger LOG = LoggerFactory.getLogger(CallCenterCommonController.class);

    @Resource
    private CallCenterCommonService callCenterCommonService;
    @Resource
    private CallCenterCallLogService callCenterCallLogService;

    /**
     * 初始化相关参数自动上传通话记录
     *
     * @param * @param null
     * @throws
     * @return
     * @author lijiangren
     * @date 2021-07-07 13 58
     * @since
     * @version
     */
    @RequestMapping(value = "/autoUploadUrl/getConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAutoUploadConfig(@RequestBody @Valid CallRecordConfigDTO callRecordConfigDTO, BindingResult br) {
        XbbResponse<CallRecordConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallRecordConfigVO callRecordConfigVO = callCenterCallLogService.getAutoUploadConfig(callRecordConfigDTO);
                response = new XbbResponse<CallRecordConfigVO>(callRecordConfigVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 保存通话记录(用于客户端主动回调)
     *
     * @param * @param null
     * @throws
     * @return 是否保存成功状态
     * @author lijiangren
     * @date 2021-07-01 11 14
     * @since
     * @version
     */
    @RequestMapping(value = "/callRecordLog/save", method = RequestMethod.POST)
    public Boolean callRecordLogSave(@RequestBody @Valid CallRecordLogSaveDTO callRecordLogSaveDTO, BindingResult br) {
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            LOG.info("回调通话记录参数异常：" + errMsg);
            return Boolean.FALSE;
        } else {
            try {
                return callCenterCallLogService.callRecordLogSave(callRecordLogSaveDTO);
            } catch (XbbException e) {
                LOG.info("回调通话记录异常：" + e.getMessage());
                return Boolean.FALSE;
            }
        }
    }

    /**
     * 获取开启的话务中心平台（一家公司只能开启一个平台）
     *
     * @return 开启的话务中心列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/module", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String module(@RequestBody @Valid CallCenterModuleDTO callCenterModuleDTO, BindingResult br) {
        XbbResponse<CallCenterModuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallCenterModuleVO callCenterModuleVO = callCenterCommonService.module(callCenterModuleDTO);
                response = new XbbResponse<>(callCenterModuleVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更换呼叫中心（一家公司只能使用一个平台）
     *
     * @return 更换呼叫中心
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/updateModuleType", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateModuleType(@RequestBody @Valid CallUpdateModuleDTO callUpdateModuleDTO, BindingResult br) {
        XbbResponse<CallUpdateModuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.updateModuleType(callUpdateModuleDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 根据呼叫手机号查找对象
     *
     * @return 根据呼叫手机号查找对象
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/findRef", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String findRef(@RequestBody @Valid CallFindRefDTO callFindRefDTO, BindingResult br) {
        XbbResponse<CallFindRefVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.findRef(callFindRefDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 一键登录
     *
     * @return 用户一键登录使用的token或提醒用户进行注册
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String login(@RequestBody @Valid CallCenterLoginDTO callCenterLoginDTO, BindingResult br) {
        XbbResponse<CallCenterLoginVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.login(callCenterLoginDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 刷新token
     *
     * @return 刷新token(用户一键登录使用的token或提醒用户进行注册)
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/token/refresh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String tokenRefresh(@RequestBody @Valid CallTokenRefreshDTO callTokenRefreshDTO, BindingResult br) {
        XbbResponse<CallTokenRefreshVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.tokenRefresh(callTokenRefreshDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取手机验证码
     *
     * @return 获取手机验证码
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/mobileCode/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileCodeSend(@RequestBody @Valid CallMobileCodeSendDTO callMobileCodeSendDTO, BindingResult br) {
        XbbResponse<CallMobileCodeSendVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.mobileCodeSend(callMobileCodeSendDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 验证手机验证码
     *
     * @return 验证手机验证码(帮助用户完成注册和登录的操作)
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/mobileCode/verify", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileCodeVerify(@RequestBody @Valid CallMobileCodeVerifyDTO callMobileCodeVerifyDTO, BindingResult br) {
        XbbResponse<CallMobileCodeVerifyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.mobileCodeVerify(callMobileCodeVerifyDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存通话记录
     *
     * @return 保存通话记录
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callLog/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callLogSave(@RequestBody @Valid CallCallLogSaveDTO callCallLogSaveDTO, BindingResult br) {
        XbbResponse<CallCallLogSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.callLogSave(callCallLogSaveDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 未接来电记录
     *
     * @return 未接来电记录
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/missedCall/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String missedCallList(@RequestBody @Valid CallMissedCallListDTO callMissedCallListDTO, BindingResult br) {
        XbbResponse<CallLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.missedCallList(callMissedCallListDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 防封号提醒
     *
     * @return 防封号提醒
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/blockRule/remind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String blockRuleRemind(@RequestBody @Valid BlockRuleRemindDTO blockRuleRemindDTO, BindingResult br) {
        XbbResponse<BlockRuleRemindVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.blockRuleRemind(blockRuleRemindDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 通话记录列表
     *
     * @return 通话记录列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callLog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callLogList(@RequestBody @Valid CallCallLogListDTO callCallLogListDTO, BindingResult br) {
        XbbResponse<CallLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallLogListVO callLogListVO = this.callCenterCallLogService.callLogList(callCallLogListDTO);
                response = new XbbResponse<>(callLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 查找度言未接来电通话记录
     *
     * @return 查找度言未接来电通话记录
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/org/missedCall/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String orgMissedCallList(@RequestBody @Valid CallCallLogListDTO callCallLogListDTO, BindingResult br) {
        XbbResponse<CallLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.orgMissedCallList(callCallLogListDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取通话记录的录音（直接拼接度言通话录音文件的路径，格式.oga）
     *
     * @return 获取通话记录的录音（直接拼接度言通话录音文件的路径，格式.oga）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callLog/record/static", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callLogRecordStatic(@RequestBody @Valid CallRecordStaticDTO callRecordStaticDTO, BindingResult br) {
        XbbResponse<CallLogRecordStaticVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.callLogRecordStatic(callRecordStaticDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取通话记录的录音（通过度言API获取文件路径，最后得到.oga的路径）
     *
     * @return 获取通话记录的录音（通过度言API获取文件路径，最后得到.oga的路径）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callLog/record/dynamic", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callLogRecordDynamic(@RequestBody @Valid CallRecordDynamicDTO callRecordDynamicDTO, BindingResult br) {
        XbbResponse<CallLogRecordStaticVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.callLogRecordDynamic(callRecordDynamicDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 同步组织架构到度言（销帮帮）
     *
     * @return 同步组织架构到度言（销帮帮）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/dep/sync", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String syncDepartment(@RequestBody @Valid CallSyncDepartmentDTO callSyncDepartmentDTO, BindingResult br) {
        XbbResponse<CallSyncDepartmentVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.syncDepartment(callSyncDepartmentDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 客户防骚扰缓存比对
     *
     * @return 客户防骚扰缓存比对
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/customer/disturb", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerDisturb(@RequestBody @Valid CallCustomerDisturbDTO callCustomerDisturbDTO, BindingResult br) {
        XbbResponse<CallCustomerDisturbVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.customerDisturb(callCustomerDisturbDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 通话录音语音转写
     *
     * @return 通话录音语音转写
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/audioTranslate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String audioTranslate(@RequestBody @Valid CallAudioTranslateDTO callAudioTranslateDTO, BindingResult br) {
        XbbResponse<CallAudioTranslateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallAudioTranslateVO callAudioTranslateVO = callCenterCallLogService.audioTranslate(callAudioTranslateDTO);
                response = new XbbResponse<>(callAudioTranslateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
